package controllers;


import play.data.validation.Required;
import play.data.validation.Valid;
import play.mvc.Before;
import play.mvc.Controller;

public class Security extends Controller{
	private static final String LOGINKEY = "LOGINNAME";
	
	@Before(unless={"verify","login","logout"})
	public static void check(){
		if(!session.contains(LOGINKEY)){
			login();
		}
	}
	
	public static void verify(@Required() String username,@Required String password,@Required String rand){		
		if(validation.hasErrors()){
			render("/Security/login.html",username,password,rand);
		}
		
		if(username.equals("dev") && password.equals("dev")){
			session.put(LOGINKEY,"DEV");
			App.index();
		}else{
			String msg = "帐号密码错误";
			render("/Security/login.html",msg,username,password,rand);
		}
	}
	
	public static void login(){
		flash.keep("url");
		render();
	}
	
	public static void logout(){
		session.clear();
		login();
	}
}
